<?php

class Upload {
	protected $outputData;
	protected $inputData;
	protected $objView;
	protected $fileSize = 1024000;

	function __construct($objView) {
		$this->objView = $objView;
	}
	
	public function setFileSize($fileSize) {
                $this->fileSize = $fileSize;
	}

	public function addFile($formFileName,$destDir,$mimeTypeArray,$newFileName) {
	        // IF $newFileName = NULL class leave the original file name
  		$this->inputData[] = array($formFileName,$destDir,$mimeTypeArray,$newFileName);
	}

	public function uploadFiles() {
	        $html = NULL;
		for ($i=0; $i<sizeof($this->inputData); $i++) {
			$returnCode = false;
			$newFileName = NULL;
		
			if (isset($_FILES[$this->inputData[$i][0]])) {

                                $html = $this->objView->wrongMimeType();

				if ($_FILES[$this->inputData[$i][0]]['error']!=0) {
					if ($_FILES[$this->inputData[$i][0]]['error']==2) {
					        $html = $this->objView->fileToBig();
					} elseif ($_FILES[$this->inputData[$i][0]]['error']==3) {
					        $html = $this->objView->incompliteFile();
					} elseif ($_FILES[$this->inputData[$i][0]]['error']==4) {
						$html = $this->objView->noFileToUpload();
					} else {
					        $html = $this->objView->unknowError();
					}
				} else {
					for ($j=0;$j<sizeof($this->inputData[$i][2]);$j++) {
						if (strcmp($_FILES[$this->inputData[$i][0]]['type'],$this->inputData[$i][2][$j]) == 0) {
							// wyjscie z petli jak typ sie zgadza
							$j = sizeof($this->inputData[$i][2]);

							if ($_FILES[$this->inputData[$i][0]]['size'] > $this->fileSize){
								$uploadData['name'] = $newFileName;
								$uploadData['size'] = round(($_FILES[$this->inputData[$i][0]]['size']/ 1000000),1);

                                                                $html = $this->objView->fileToBig($uploadData);
       							} else {
								if (is_uploaded_file($_FILES[$this->inputData[$i][0]]['tmp_name'])) {
									$orginalFileName = explode(".",$_FILES[$this->inputData[$i][0]]['name']);
									$FileExtention = $orginalFileName[1];
									if (is_null($this->inputData[$i][3])) {
        									$newFileName = $orginalFileName[0].'.'.$orginalFileName[1];
									} else {
										$newFileName = $this->inputData[$i][3].'.'.$FileExtention;
									}

									if (move_uploaded_file($_FILES[$this->inputData[$i][0]]['tmp_name'],$this->inputData[$i][1].$newFileName)){
										$uploadData['name'] = $newFileName;
										$uploadData['size'] = round(($_FILES[$this->inputData[$i][0]]['size']/ 1000000),1);

								        	$html = $this->objView->successfullUpload($uploadData);
										$returnCode = true;
									} else {
									        $html = $this->objView->copyFileError();
									}
								} else {
								        $html = $this->objView->loadFileError();
								}
							}
						}
					}
				}
				unset($_FILES[$this->inputData[$i][0]]);
			} else {
				$html = $this->objView->noFileToUpload();
			}
			$this->outputData[$this->inputData[$i][0]] = array($returnCode,$html,$newFileName);
		}
	}

	public function isSuccess($fileVarName) {
		return $this->outputData[$fileVarName][0];
	}
	
	public function getReturnMessage($fileVarName) {
		return $this->outputData[$fileVarName][1];
	}
	
	public function getNewFileName($fileVarName) {
		return $this->outputData[$fileVarName][2];
	}

	public function getResult() {
		return $this->outputData;
	}
}

?>
